package com.jiaoxn.example.controller;


import com.jiaoxn.example.common.response.structure.ResponseStructure;
import com.jiaoxn.example.controller.param.SimpleCompanyParam;
import com.jiaoxn.example.entity.CompanyDO;
import com.jiaoxn.example.service.impl.CompanyServiceImpl;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jiao xn
 * @date 2022/8/25
 */
@Api(tags = "公司")
@RestController
@RequestMapping("/company")
public class CompanyController {
    private final CompanyServiceImpl companyServiceImpl;

    @Autowired
    public CompanyController(CompanyServiceImpl companyServiceImpl) {
        this.companyServiceImpl = companyServiceImpl;
    }

    @ApiOperation(value = "通过ID查询单条数据")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @GetMapping("{id}")
    public ResponseEntity<CompanyDO> queryById(
            @PathVariable(name = "id") Integer id
    ) {

        return ResponseEntity.ok(companyServiceImpl.queryById(id));
    }

    @ApiOperation(value = "新增公司信息")
    @ApiResponses({
            @ApiResponse(code = 201, message = "新增成功"),
            @ApiResponse(code = 406, message = "参数校验失败"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @ResponseStatus(value = HttpStatus.CREATED)
    @PostMapping("")
    public ResponseStructure<Object> insertCompany(@RequestBody @Validated SimpleCompanyParam simpleCompanyParam) {
        Integer newCompanyId = companyServiceImpl.insertCompany(simpleCompanyParam.getName());

        Map<String, Integer> newCompanyInfo = new HashMap<>();
        newCompanyInfo.put("id", newCompanyId);

        ResponseStructure<Object> responseStructure = new ResponseStructure<>();
        responseStructure.setData(newCompanyInfo);
        return responseStructure;
    }
}
